package com.alibaba.citrus.cr.platform.enhance.repository.impl;

import javax.annotation.Resource;

import com.alibaba.citrus.cr.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.cr.platform.enhance.repository.StrategyWriteRepository;
import com.alibaba.fastjson.JSONObject;

import com.epoch.app.crplatformenhance.dimension.service.DimensionService;
import com.epoch.app.crplatformenhance.sdo.StrategySDO;
import com.epoch.app.crplatformenhance.strategy.dto.CreateStrategyResponse;
import com.epoch.app.crplatformenhance.strategy.dto.DeleteStrategyRequest;
import com.epoch.app.crplatformenhance.strategy.dto.DeleteStrategyResponse;
import com.epoch.app.crplatformenhance.strategy.dto.UpdateStrategyResponse;
import com.epoch.app.crplatformenhance.strategy.model.Strategy;
import com.epoch.app.crplatformenhance.strategy.service.StrategyService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Component
public class StrategyWriteRepositoryImpl implements StrategyWriteRepository {

    @Resource
    private StrategyService strategyService;

    @Resource
    private DimensionService dimensionService;

    @Override
    @RepositoryInvoker
    @Transactional(rollbackFor = {Exception.class,RuntimeException.class})
    public String save(StrategySDO strategySDO) {
        Strategy strategy = strategyConverter(strategySDO);
        CreateStrategyResponse response = null;
        response = strategyService.createStrategy(strategy);
//        List<Dimension> dimensionList = dimensionConverter(strategySDO, response.getLastInsertId());
//            dimensionList.stream().forEach(dimension ->
//                    dimensionService.createDimension(dimension)
//            );
        return response.getLastInsertId();
    }

//    private List<Dimension> dimensionConverter(StrategySDO strategySDO, String lastInsertId) {
//        List<Dimension> dimensions = strategySDO.getDimensionSDOList().stream().map(dimensionSDO -> {
//            Dimension dimension = new Dimension();
//            dimension.setCode(dimensionSDO.getCode());
//            dimension.setCodeName(dimensionSDO.getCodeName());
//            dimension.setValue(dimensionSDO.getValue());
//            dimension.setRuleName(dimensionSDO.getRuleName());
//            dimension.setRuleValue(dimensionSDO.getRuleValue());
//            dimension.setStrategyId(lastInsertId);
//            dimension.setStatus(StatusEnum.open.getCode());
//            dimension.setValidBeginTime(dimensionSDO.getValidBeginTime());
//            dimension.setValidEndTime(dimensionSDO.getValidEndTime());
//            dimension.setExecuteAction(dimensionSDO.getExecuteAction());
//            dimension.setDetails(dimensionSDO.getDetails());
//            return dimension;
//        }).collect(Collectors.toList());
//
//        return dimensions;
//    }


    @Override
    public String delete(StrategySDO strategySDO) {
        DeleteStrategyRequest deleteStrategyRequest = new DeleteStrategyRequest();
        if (strategySDO.getId() != null) {
            deleteStrategyRequest.setId(Long.parseLong(strategySDO.getId()));
        }
        DeleteStrategyResponse deleteStrategyResponse = strategyService.deleteStrategy(deleteStrategyRequest);
        if (deleteStrategyResponse.getCount() != 1) {
            throw new RuntimeException("delete StrategySDO effect rows not 1!");
        }
        return strategySDO.getId();

    }

    @Override
    public String modify(StrategySDO strategySDO) {
        Strategy strategy = strategyConverter(strategySDO);
        UpdateStrategyResponse updateStrategyResponse = strategyService.updateStrategy(strategy);
//        List<Dimension> dimensionList = dimensionConverter(strategySDO, strategySDO.getId());
//        dimensionList.stream().forEach(dimension ->
//                dimensionService.createDimension(dimension)
//        );
        if (updateStrategyResponse.getCount() != 1) {
            throw new RuntimeException("modify StrategySDO effect rows not 1!");
        }
        return strategySDO.getId();
    }

    @Override
    public String edit(StrategySDO strategySDO) {
        Strategy strategy = strategyConverter(strategySDO);
        UpdateStrategyResponse updateStrategyResponse = strategyService.updateStrategy(strategy);
        if (updateStrategyResponse.getCount() != 1) {
            throw new RuntimeException("modify StrategySDO effect rows not 1!");
        }
        return String.valueOf(updateStrategyResponse.getCount()) ;
    }

    private Strategy strategyConverter(StrategySDO strategySDO) {
        Strategy strategy = new Strategy();
        if (!StringUtils.isEmpty(strategySDO.getId())) {
            strategy.setId(Long.parseLong(strategySDO.getId()));
        }
        if (null != strategySDO.getGmtCreate()) {
            strategy.setGmt_create(strategySDO.getGmtCreate());
        }
        if (null != strategySDO.getGmtModified()) {
            strategy.setGmt_modified(strategySDO.getGmtModified());
        }
        strategy.setShare(strategySDO.getShare());
        strategy.setPriority(strategySDO.getPriority());
        strategy.setDimensionType(strategySDO.getDimensionType());
        strategy.setName(strategySDO.getName());
        strategy.setStatus(strategySDO.getStatus());
        strategy.setCreator(strategy.getCreator());
        strategy.setValidBeginTime(strategySDO.getValidBeginTime());
        strategy.setValidEndTime(strategySDO.getValidEndTime());
        strategy.setType(strategySDO.getType());
        strategy.setOrganizationCode(strategySDO.getOrganizationCode());
        strategy.setOrganizationName(strategySDO.getOrganizationName());
        strategy.setDailyBeginTime(strategySDO.getDailyBeginTime());
        strategy.setDailyEndTime(strategySDO.getDailyEndTime());
        if (null != strategySDO.getFeature()) {
            strategy.setFeature(JSONObject.toJSONString(strategySDO.getFeature()));
        }
        return strategy;
    }
}
